﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Nirvana
{
    /// <summary>
    /// 带可设颜色的外边沿：描边和阴影，比自带的outline和shadow组件效果要好
    /// </summary>
    [AddComponentMenu("Nirvana/UI/Effects/Outer Bevel")]
    [RequireComponent(typeof(RectTransform))]
    public class UIOuterBevel : BaseMeshEffect
    {
        [SerializeField]
        private Color highlightColor = Color.white;
        [SerializeField]
        private Color shadowColor = Color.black;
        [SerializeField]
        private Vector2 bevelDirectionAndDepth = Vector2.one;
        [SerializeField]
        private bool useGraphicAlpha = true;

#if UNITY_EDITOR
        protected override void OnValidate()
        {
            if (base.graphic != null) base.graphic.SetVerticesDirty();
        }
#endif

        public override void ModifyMesh(VertexHelper vh)
        {
            if (!this.IsActive() || vh.currentVertCount <= 0)
            {
                return;
            }

            List<UIVertex> tempList = VertexLists.Get();
            vh.GetUIVertexStream(tempList);
            List<UIVertex> newlist = ModifyTextVertexs(tempList);
            if (newlist != null)
            {
                vh.Clear();
                vh.AddUIVertexTriangleStream(newlist);
            }
            VertexLists.Set(newlist);
        }

        private List<UIVertex> ModifyTextVertexs(List<UIVertex> _list)
        {
            int totalCount = _list.Count;


            this.ModifyVertex(_list, this.shadowColor, 0, _list.Count, this.bevelDirectionAndDepth.x * 0.75f, -this.bevelDirectionAndDepth.y * 0.75f);
            this.ModifyVertex(_list, this.shadowColor, 0, _list.Count, this.bevelDirectionAndDepth.x, this.bevelDirectionAndDepth.y * 0.5f);
            this.ModifyVertex(_list, this.shadowColor, 0, _list.Count, -this.bevelDirectionAndDepth.x * 0.5f, -this.bevelDirectionAndDepth.y);
            this.ModifyVertex(_list, this.highlightColor, 0, _list.Count, -this.bevelDirectionAndDepth.x, this.bevelDirectionAndDepth.y * 0.5f);
            this.ModifyVertex(_list, this.highlightColor, 0, _list.Count, -this.bevelDirectionAndDepth.x * 0.5f, this.bevelDirectionAndDepth.y);

            if (this.HasComponent<UIMaterialEffect>())
            {
                for (int i = 0; i < _list.Count - totalCount; i++)
                {
                    UIVertex _vert = _list[i];
                    _vert.uv1 = new Vector2(0, 0);
                    _list[i] = _vert;

                }
            }

            return _list;
        }


        private void ModifyVertex(List<UIVertex> _list, Color32 effectColor, int startpos, int Count, float xOffset, float yOffset)
        {
            for (int i = startpos; i < Count; i++)
            {
                UIVertex vertex = _list[i];
                _list.Add(vertex); //重影
                Vector3 pos = vertex.position;
                pos.x += xOffset;
                pos.y += yOffset;
                vertex.position = pos;
                if (this.useGraphicAlpha)
                {
                    effectColor.a = (byte)(effectColor.a * _list[i].color.a / byte.MaxValue);
                }
                vertex.color = effectColor;
                _list[i] = vertex;
            }
        }
    }
}



